import { Component, OnInit } from '@angular/core';

import { SysinfoService } from './sysinfo.service';

@Component({
  selector: 'app-sysinfo',
  templateUrl: './sysinfo.component.html',
  styleUrls: ['./sysinfo.css'],
  providers: [
    SysinfoService
  ]
})
export class SysinfoComponent implements OnInit {
  sysInfo = {
    hasMem: 0,
    totalMem: 0,
    memPercent: 0,
    sysTime: 0
  };

  constructor(
    private _sysinfoService: SysinfoService
  ) { }

  ngOnInit() {
    this.getSysinfo();
  }

  getSysinfo() {
    this._sysinfoService.getSysinfo()
      .subscribe(data => {
        if (data.status === 1) {
          this.sysInfo.hasMem = (data.data.totalMem - data.data.freeMem) / 1024 / 1024;
          this.sysInfo.totalMem = data.data.totalMem / 1024 / 1024;
          this.sysInfo.memPercent = this.sysInfo.hasMem / this.sysInfo.totalMem * 100;
          this.sysInfo.sysTime = data.data.sysTime;
          setInterval(() => {
            this.sysInfo.sysTime += 1000;
          }, 1000);
        } else {

        }
      }, error => {
        console.error(error);
      });
  }
}
